<?php
/**
 * 应用（品类）
 * User: bgl
 * Date: 2023/6/20
 * Time: 11:14
 */

namespace App\Admin\Controllers\Products;


use App\Admin\Extensions\Grid\Tool\Import;
use App\Models\Product\ProductMaterialApplicationClassify;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;

class MaterialApplicationClassifyController extends AdminController{

    protected $title = '应用品类';

    protected function grid(){

        $grid = new Grid(new ProductMaterialApplicationClassify());
        $grid->column('id', 'ID');
        $grid->column('code', '品类代码(缩写)');
        $grid->column('title', '品类名称');
        $grid->column('title_en', '品类名称(英文)');

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableDelete();
        });
        $grid->tools(function (Grid\Tools $tools) {
            $tools->append(new Import('products/material-application-classify-upload?type=classify'));
        });

        $grid->disableRowSelector();
        $grid->disableExport();
        $grid->expandFilter();
        $grid->filter(function($filter){
            $filter->column(1/3, function ($filter){
                $filter->where(function ($query) {
                    $query->where('code', 'like', "%{$this->input}%")
                        ->orWhere('title', 'like', "%{$this->input}%");
                }, '查询','search8')->placeholder('模糊查询品类代码、名称');
            });
        });

        return $grid;
    }

    public function form(){

        $form = new Form(new ProductMaterialApplicationClassify());

        $form->hidden('id');
        $form->select('parent_id', '应用系统')->options(ProductMaterialApplicationClassify::$applicationType)
            ->help('选择应用大类(固定项)');

        $form->text('code', '品类代码(缩写)')->pattern('[A-Z]{3}')->rules('required')
            ->help('品类英文的三位(大写字母)组合，例：帐篷(Tent)，可标识为：Ten')
            ->creationRules(['required', "unique:product_material_classify"])
            ->updateRules(['required', "unique:product_material_classify,code, {{id}}"]);

        $form->text('title', '品类名称')->rules('required')
            ->help('输入应用品类中文，例：帐篷')
            ->creationRules(['required', "unique:product_material_classify"])
            ->updateRules(['required', "unique:product_material_classify,title, {{id}}"]);

        $form->text('title_en', '品类名称(英文)')->rules('required')
            ->help('输入应用品类英文，例：Tent')
            ->creationRules(['required', "unique:product_material_classify"])
            ->updateRules(['required', "unique:product_material_classify,title_en, {{id}}"]);

        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
            $tools->disableDelete();
        });

        return $form;
    }

}
